package com.itheima.reggie.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entify.Category;
import com.itheima.reggie.entify.Setmeal;
import com.itheima.reggie.entify.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

//套餐管理
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增套餐
     */
    @PostMapping
    public R saveSetmeal(@RequestBody SetmealDto setmealDto) {

        //调用方法并返回

        return setmealService.saveSetmeal(setmealDto);
    }

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        //创建分页对象和构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> PageInfo1 = new Page<>();
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();

        //创建分页条件
        qw.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        setmealService.page(pageInfo, qw);

        long total = pageInfo.getTotal();
        BeanUtils.copyProperties(pageInfo, PageInfo1, "records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(setmeal, setmealDto);
            Long categoryId = setmealDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        PageInfo1.setRecords(list);
        return R.success(PageInfo1);
    }

    /**
     * 删除套餐接口
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {

        boolean result = setmealService.deleteWithDish(ids);

        return result ? R.success("删除套餐成功") : R.error("套餐正在售卖中，不能删除！！！！");
    }

    /**
     * 套餐售卖状态
     */
    @PostMapping("/status/{status}")
    public R status(@RequestParam List<Long> ids ,Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);
        setmealService.update(setmeal,qw);
        return  R.success("修改状态成功");
    }
     /**
     * 根据套餐id查询套餐数据
     */
     @GetMapping("/{id}")
     public R selectById(@PathVariable Long id){

         SetmealDto byIdSetmeal = setmealService.getByIdSetmeal(id);

         return R.success(byIdSetmeal);
     }

    /**
     * 修改套餐接口
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 根据套餐分类id查询该套餐分类下的套餐
     */
    @GetMapping("/list")
    public R getSetmealByCategoryId( Long categoryId){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }

}